<template>
  <div>
    <!-- 面包屑导航区域 -->
    <a-breadcrumb style="margin: 0px 0px 0px 0px">
          <a-breadcrumb-item>首页</a-breadcrumb-item>
          <a-breadcrumb-item>个人信息</a-breadcrumb-item>
          <a-breadcrumb-item>专业技能</a-breadcrumb-item>
    </a-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="bg">
    <el-row :gutter="20" style="margin-top:15px"> 
        <el-col :span="7">
            <el-input placeholder="请输入搜索内容" 
            v-model="queryInfo.query" clearable @clear="userlist">
            <el-button slot="append" icon="el-icon-search" @click="userlist"></el-button>
    </el-input>
        </el-col>
        <el-col :span="2">
            <el-button type="primary" @click="addDialogVisible=true">添加</el-button>
        </el-col>
    </el-row>
    <!-- 用户列表区域 -->
    <el-table :data="userlist" border stripe style="margin-top:10px">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="技能名称" prop="skillname"></el-table-column>
        <el-table-column label="使用日期" prop="skilldate"></el-table-column>
        <el-table-column label="熟练程度" prop="skillcd"></el-table-column>
        <el-table-column label="备注" prop="beizhu"></el-table-column>
        <el-table-column label="角色" prop="role_name"></el-table-column>
        <el-table-column label="状态" prop="mg_state">
            <template slot-scope="scope">
                <el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)">
                </el-switch>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
            <template >
                <!-- 修改按钮 -->
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog()"></el-button>
                <!-- 删除按钮 -->
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeSkillById()" ></el-button>
                <!-- 分配角色按钮 -->
                <el-tooltip effect='dark' content="分配角色" placement="top" :enterable="false">
                    <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
                </el-tooltip>
                
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="userlist.length">
    </el-pagination>
    </el-card>

    <!-- 添加技能的对话框 -->
    <el-dialog
    title="新增技能"
    :visible.sync="addDialogVisible"
    width="50%" @close="addDialogClosed">
    <!-- 内容主体区 -->
    <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" width="50px">
        <el-form-item label="新技能" prop="skillname" >
        <el-input v-model="addForm.skillname"></el-input>
        </el-form-item>
        <el-form-item label="使用日期" prop="skilldate" >
        <el-input v-model="addForm.skilldate"></el-input>
        </el-form-item>
        <el-form-item label="熟练程度" prop="skillcd">
        <el-input v-model="addForm.skillcd"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="beizhu">
        <el-input v-model="addForm.beizhu"></el-input>
        </el-form-item>
    </el-form>
    <!-- 底部区域 -->
    <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSkill">确 定</el-button>
    </span>
    </el-dialog>
    <!-- 修改技能的对话框 -->
    <el-dialog title="修改技能" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
    <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" width="50px">
        <el-form-item label="新技能" prop="skillname" >
        <el-input v-model="editForm.skillname"></el-input>
        </el-form-item>
        <el-form-item label="使用日期" prop="skilldate" >
        <el-input v-model="editForm.skilldate"></el-input>
        </el-form-item>
        <el-form-item label="熟练程度" prop="skillcd">
        <el-input v-model="editForm.skillcd"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="beizhu">
        <el-input v-model="editForm.beizhu" disabled></el-input>
        </el-form-item>
    </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editSkillInfo">确 定</el-button>
  </span>
</el-dialog>
  </div>

</template>

<script>
export default {
    data(){
        return {
            //获取用户列表的参数对象
            queryInfo:{
                query:'',
                //当前的页数
                pagenum:1,
                //当前每页显示多少条数据
                pagesize:2
            },
            userlist:[
                {
                    skillname:"admin",
                    skilldate:"11211@qq.com",
                    skillcd:"1234566"
                },
                {
                    skillname:"admin",
                    skilldate:"11211@qq.com",
                    skillcd:"123456"
                },
                {
                    skillname:"admin",
                    skilldate:"11211@qq.com",
                    skillcd:"123456"
                },
                {
                    skillname:"admin1",
                    skilldate:"11211@qq.com",
                    skillcd:"123456"
                },
                {
                    skillname:"admin1",
                    skilldate:"11211@qq.com",
                    skillcd:"123456"
                }
            ],
            total:0,
            //控制添加技能对话框的显示与隐藏
            addDialogVisible:false,
            //添加技能表单数据
            addForm:{
                skillname:'',
                skilldate:'',
                skillcd:'',
                beizhu:''
            },
            //添加表单的验证规则对象
            addFormRules:{
                skillname:[
                    {
                        required:true,
                        message:"请输入新技能",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ],
                skilldate:[
                    {
                        required:true,
                        message:"请输入新技能使用日期",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ],
                skillcd:[
                   {
                        required:true,
                        message:"请输入新技能熟练程度",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ],
                beizhu:[
                    {
                        required:true,
                        message:"请输入备注内容",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ]
            },
            //控制修改技能对话框的显示与隐藏
            editDialogVisible:false,
            //查询到的技能信息对象
            editForm:{},
            //修改表单的验证规则对象
            editFormRules:{
                 skillname:[
                    {
                        required:true,
                        message:"请输入新技能",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ],
                skilldate:[
                    {
                        required:true,
                        message:"请输入新技能使用日期",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ],
                skillcd:[
                   {
                        required:true,
                        message:"请输入新技能熟练程度",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ],
                beizhu:[
                    {
                        required:true,
                        message:"请输入备注内容",
                        tirgger:'blur'
                    },
                    {
                        min:5,max:10,
                        message:"输入的字符长度应在5~10个字符之间",
                        tirgger:'blur'
                    }
                ]
            }

        }
    },
    // created(){
    //     this.getUserList()
    // },
    methods: {
        // async getUserlist(){
        //     const {data:res} = await this.$http.get("users",{
        //         params:this.queryInfo
        //         })
        //         if(res.meta.status !== 200){
        //             return this.$message.error('获取用户列表失败！')
        //         }
        //         this.userlist=res.data.users 
        //         this.total=res.data.total
        // },
         //监听 pagesize 改变的事件
        handleSizeChange(newSize){
            console.log(newSize)
            this.queryInfo.pagesize=newSize
            // this.userlist()
        },
        //监听 页码值 改变的事件
        handleCurrentChange(newPage){
            console.log(newPage)
            this.queryInfo.pagenum=newPage
            // this.userlist()
        },


        //监听添加技能对话框的关闭事件
        addDialogClosed(){
            this.$refs.addFormRef.resetFields()
        },

        //点击按钮，添加新技能
        addSkill(){
            this.$refs.addFormRef.validate(async valid=>{
                if(!valid) return
                //可以发起添加技能的网络请求
                const {data:res}=await this.$http.post('users',this.addForm)
                if(res.meta.status!=201){
                    this.$message.error('添加失败！')
                }
                this.$message.success('添加成功！')
                //隐藏添加技能的对话框
                this.addDialogVisible=false
                //重新获取列表数据
                this.getUserlist()

            })

        },
        //展示编辑技能的对话框
         showEditDialog(){
            // const {data:res}=await this.$http.get('users/'+id)
            // if(res.meta.status!==200){
            //     return this.$message.error('查询失败！')
            // }
            // this.editForm=res.data 
            this.editDialogVisible=true
        },
        //监听修改技能对话框的关闭事件
        editDialogClosed(){
            this.$refs.editFormRef.resetFields()
        },
        //修改信息并提交
        editSkillInfo(){
            this.$refs.editFormRef.validate(async valid=>{
                 if(!valid) return
                //可以发起修改技能的网络请求
                const {data:res}=await this.$http.put('users',this.editForm,{
                    skillname:this.editForm.skillname,
                    skilldate:this.editForm.skilldate,
                    skillcd:this.editForm.skillcd,
                })
                if(res.meta.status!=201){
                    this.$message.error('添加失败！')
                }
                this.$message.success('添加成功！')
                //隐藏修改技能的对话框
                this.editDialogVisible=false
                //重新获取列表数据
                this.getUserlist()

            })
        },
        //根据Id删除对应的用户信息
        async removeSkillById(){
        //弹框询问用户是否删除数据
         const confirmResult =  await this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        //如果用户确认删除，则返回值为字符串confirm
        //如果用户取消删除，则返回值为字符串cancle
        console.log(confirmResult)
        // if(confirmResult !== 'confirm'){
        //     return this.$message.info('已取消删除')
        // }
        // this.$http.delete("users/"+id)
        // if(res.meta.status !== 200){
        //     return this.$message.error('删除用户失败！')
        // }
        // this.$message.success('删除用户成功！')
        // this.getUserlist()
        },
        deleteRow(index, rows) {
        rows.splice(index, 1);
      }
        }
    }
</script>

<style lang="less" scoped>
    .bg{
        margin-top:20px;
        background-color: #e9eef3;
    }
    label,div{
        font-size:14px;
    }
    
</style>